/*
 * BanBu_PersonDataHeader
 * @description 本文件提供个人空间的headerView
 * @package
 * @author 		***
 * @copyright
 * @version 		1.0
 * @description 本文件提供个人空间的headerView
 */


#import "BanBu_PersonDataHeader.h"
#import "BanBu_PersonMusicPlay.h"
#import "BanBu_SnowFall.h"
#import "AppDataManager.h"
#import "BanBu_PhotoShowViewController.h"
#import "UIImageView+WebCache.h"
#import "BanBu_AlbumModel.h"
#import "BanBu_HisFriendViewController.h"
#import "QuadCurveMenuItem.h"
#import "Profile.h"
#import "UIViewController+Animation.h"
@implementation BanBu_PersonDataHeader

+(BanBu_PersonDataHeader *)instanceView
{
    return [[[NSBundle mainBundle] loadNibNamed:@"BanBu_PersonDataHeader" owner:nil options:nil] objectAtIndex:0];
}

#pragma mark - 判断是不是自己
/**
 *	@brief	判断是不是自己
 *
 *	@return	bool
 */
- (BOOL)isSelf
{
    return [[_dataSource valueForKey:@"userid"] isEqualToString:MyAppDataManager.useruid];
}


#pragma mark - 设置动态插件的更改通知
-(void)changeFloatImage:(NSNotification *)_note
{
    [self setFloatImage:[_note object]];
}
#pragma mark - 设置动态插件的url
-(void)setFloatImage:(NSString *)_urlString
{
    snow.flakeImageURL = _urlString;
}
/**
 *	@brief 进入背景图片选择
 */

#pragma mark - 点击头像背景图更换
- (void)choseWavePhoto
{
    UIViewController *v = (UIViewController *)_delegate;
    [ProfileOpManager setWaveImageForViewController:v];
    [ProfileOpManager setWaveImageModified:^(BOOL succeed) {
        if(succeed)
        {
            [[NSNotificationCenter defaultCenter]postNotificationName:NOTIFICATION_REFRESH_PROFIL object:nil];
        }
    }];
    
    //    UIActionSheet *choose = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"chooseWaveImage", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"取消", nil) destructiveButtonTitle:nil otherButtonTitles:NSLocalizedString(@"funLabel", nil),NSLocalizedString(@"funLabel1", nil), nil];
    //    [choose showInView:self];
    //    [choose release];
}
/**
 *	@brief BanBu_AvatarMenu 的delegate
 *
 *	@param 	menu
 *	@param 	idx
 */
- (void)quadCurveMenu:(QuadCurveMenu *)menu didSelectIndex:(NSInteger)idx

{
    
}
/**
 *	@brief	头像点击跳转
 *
 *	@param 	sender
 */
- (IBAction)avatarPhotoPress:(UIButton *)sender
{
    //    menu.expanding = !menu.expanding;
    BanBu_AlbumModel * albumModel = [[BanBu_AlbumModel alloc]init];
    
    albumModel.album_id    = Album_face;

    albumModel.album_title = NSLocalizedString(@"album_face", nil);

    albumModel.image_url   = @"http://imgt8.bdstatic.com/it/u=2,926634916&fm=19&gp=0.jpg";

    albumModel.user_id     = [_dataSource valueForKey:@"userid"];

    albumModel.numCount    = 10;
    
    BanBu_PhotoShowViewController * photoShow = [[BanBu_PhotoShowViewController alloc]init];
    photoShow.albumModel = albumModel;
    UIViewController *v = (UIViewController *)_delegate;
    [v.navigationController pushViewController:photoShow animated:YES];
    [photoShow release];
    
}
-(void)setDataSource:(NSDictionary *)dataSource
{
    _dataSource = [dataSource retain];
    //    添加头像附加功能栏 BanBu_AvatarMenu 暂时隐藏
    //    [self addMenuView];
    if(_dataSource)
    {
        
        NSString *waveurl = [[[_dataSource valueForKey:@"spaceset"] valueForKey:@"space_json"] valueForKey:KEY_WAVE_PICTURE];
        
        if([self isSelf])
        {
            [self.waveImage setCanEdit:YES];
            [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeFloatImage:) name:Notification_FloatImage_Change object:nil];
        }
        NSInteger row = [[dataSource valueForKey:@"userid"] integerValue] % 10;
        self.waveImage.image = [UIImage imageNamed:[NSString stringWithFormat:@"wavedefault-%d.jpg",row]];
        if(waveurl&&waveurl.length)
        {
           
            self.waveImage.imageURL = waveurl;
        }
        
        __block BanBu_PersonDataHeader *header = self;
        [self.waveImage setWaveViewTap:^{
            [header choseWavePhoto];
        }];

        //雪花飘动图片URL
        
        self.snowImageURL = [[[_dataSource valueForKey:@"spaceset"] valueForKey:@"space_json"] valueForKey:KEY_PICTURE];
        snow.flakeImageURL = self.snowImageURL;
        //语音播放的URL
        self.loadurl = [[[_dataSource valueForKey:@"spaceset"] valueForKey:@"space_json"] valueForKey:KEY_VOICE];
        self.ufaceImage.layer.cornerRadius = 40;
        self.ufaceImage.layer.masksToBounds = YES;
//        self.ufaceImage.layer.borderColor = [[UIColor whiteColor] CGColor];
//        self.ufaceImage.layer.borderWidth = 2.0f;
        
        [self.ufaceImage setImageWithURL:[NSURL URLWithString:[_dataSource valueForKey:@"uface"]] placeholderImage:[UIImage imageNamed:@"default.png"] andType:0];
        
        NSLog(@"%@",[_dataSource valueForKey:@"uface"]);
        //    "friendShip" = "关注";
        //    "friendShip1" = "粉丝";
        
        _albumButton.titleLabel.numberOfLines = 2;
        _albumButton.titleLabel.font = [UIFont systemFontOfSize:16.0];
        NSString * albumNum = [NSString stringWithFormat:@"%@(%d)",NSLocalizedString(@"funLabel1", nil),[[dataSource valueForKey:@"albumphotosum"] integerValue]];
        [_albumButton setTitle:albumNum forState:UIControlStateNormal];
        //获取新的动态数量
        NSInteger actionCount = [[dataSource valueForKey:@"actioncount"] integerValue];
        //没有新的动态
        if (actionCount == 0)
        {
            [_actionButton setTitle:NSLocalizedString(@"dynamic", nil) forState:UIControlStateNormal];
        }
        else
        {
            [_actionButton setTitle:[NSString stringWithFormat:@"%@ (%d)",NSLocalizedString(@"dynamic", nil),actionCount] forState:UIControlStateNormal];
        }

        
        [_profileButton setTitle: NSLocalizedString(@"myinfoLabel", nil) forState:UIControlStateNormal];
        [_profileButton setSelected:YES];
        
        //按钮title
        [_fensiBtn setTitle:NSLocalizedString(@"friendShip1", nil) forState:UIControlStateNormal];
        [_guanzhuBtn setTitle:NSLocalizedString(@"friendShip", nil) forState:UIControlStateNormal];
        //粉丝与关注数目
        _fensiNum.text = [[_dataSource valueForKey:@"friendsum"] valueForKey:@"f"];
        _guanzhuNum.text = [[_dataSource valueForKey:@"friendsum"] valueForKey:@"g"];
        
        //是否隐藏关注
        BOOL    hidefollow = NO;
        if([[_dataSource allKeys] indexOfObject:@"hidefollow"] != NSNotFound)
        {
            hidefollow = [[_dataSource valueForKey:@"hidefollow"] boolValue];
        }
        
        //隐藏关注按钮
        if(hidefollow)
        {
            _guanzhuBtn.hidden = YES;
            [_guanzhuNum setHidden:YES];
        }
        
        //性别
        NSString * sex = [_dataSource valueForKey:@"gender"];
        NSString *sexpath = [[NSBundle mainBundle] pathForResource:sex ofType:@"png"];
        _sex.image = [UIImage imageWithContentsOfFile:sexpath];
        //vip
        if ([[_dataSource valueForKey:@"userlevel"] integerValue]>0)
        {
            NSString *path = [[NSBundle mainBundle] pathForResource:@"verify_vip" ofType:@"png"];
            _vip.image = [UIImage imageWithContentsOfFile:path];
        }
        //视频认证
        else if ([[_dataSource valueForKey:@"verify"] boolValue])
        {
            _verified.hidden = NO;
            NSString * verifySource = [NSString stringWithFormat:@"verify_%@",sex];
            NSString *path = [[NSBundle mainBundle] pathForResource:verifySource ofType:@"png"];
            _vip.image =  [UIImage imageWithContentsOfFile:path];
        }
        else
        {
            NSString *path = [[NSBundle mainBundle] pathForResource:@"verify_normal" ofType:@"png"];
            _vip.image =  [UIImage imageWithContentsOfFile:path];
            _verified.hidden = YES;
        }
        
        //数据显示
        _starAndAge.text = [NSString stringWithFormat:@"%@ %@",[MyAppDataManager IsInternationalLanguage:[_dataSource valueForKey:@"sstar"]],[_dataSource valueForKey:@"oldyears"]];
        NSString *interLtime = nil;
//        if([self isSelf])
//        {
//            interLtime = [MyAppDataManager getTheLastOnlineTime:[_dataSource valueForKey:@"ltime"]];
//
//        }
//        else
//        {
//            interLtime = [_dataSource valueForKey:@"ltime"];
//
//        }
        interLtime = [MyAppDataManager getTheLastOnlineTime:[_dataSource valueForKey:@"ltime"]];

        NSString *interDmeter = [MyAppDataManager IsInternationalLanguage:[_dataSource valueForKey:@"dmeter"]];
        _distance.text = interDmeter;
        _laseOnline.text = interLtime;
        
        
        //        _loadurl = [_dataSource valueForKey:<#(NSString *)#>]
    }
}
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
    }
    return self;
}

-(id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if(self)
    {
    }
    return self;
}

- (void)setDelegate:(id<PerosonDataHeaderDelegate>)delegate
{
    _delegate = delegate;
    
    
    [self playSnowFallAnimation:animbtn];
    
    /**/
    [_playbtn setMode:MusicPlayStopedMode];
    /**/
}

/**
 *	@brief	音乐按钮方法 控制背景音乐的播放暂停
 *
 *	@param 	sender 	_playbtn
 */
- (IBAction)startOrStopPlay:(id)sender
{
    if(!(_loadurl&&_loadurl.length))
    {
        [TKLoadingView showTkloadingAddedTo:self title:NSLocalizedString(@"novoiceNotice", nil) activityAnimated:NO duration:1];
        return;
    }
    
    
    _player = [BanBu_PersonMusicPlay sharePlayer];
    _player.delegate = self;
    _player.loadUrl = _loadurl;
    
    
    if([_player isPause])
    {
        [_player resume];
    }
    else if(![_player playing])
    {
        [_playbtn setMode:MusicPlayLoadingMode];
        [_player startPlay];
    }
    else
    {
        [_playbtn setMode:MusicPlayStopedMode];
        [_player pause];
    }
}
- (IBAction)buttonPress:(UIButton *)sender
{
    [_albumButton setSelected:NO];
    [_actionButton setSelected:NO];
    [_profileButton setSelected:NO];
    [sender setSelected:YES];
    
    if([_delegate respondsToSelector:@selector(headerButtonDidPressWithIndex:)])
    {
        [_delegate headerButtonDidPressWithIndex:sender.tag-10000];
    }
}
- (UIViewController *)viewController{
    id nextResponder = [self.superview nextResponder];
    if ([nextResponder isKindOfClass:[UIViewController class]]) {
        return nextResponder;
    } else {
        return nil;
    }
}

/**
 *	@brief	开始或暂停雪花飘动 默认开启
 *
 *	@param 	sender 	button
 */
- (IBAction)playSnowFallAnimation:(UIButton *)sender
{
    if(!snow)
    {
        BanBu_PersonDataViewController *v = (BanBu_PersonDataViewController *)_delegate;
        NSLog(@"%d",snow.retainCount);
        snow = [[BanBu_SnowFall alloc] initWithFrame:CGRectMake(0, 0, __MainScreen_Width, __MainScreen_Height)];
        snow.flakeImageURL = self.snowImageURL;
//        [self addSubview:snow];
//        [v.tableView.superview addSubview:snow];
        [v.view addSubview:snow];
//        [v.navigationController.view addSubview:snow];

        
    }
    if(!sender.selected)
    {
        [sender setBackgroundImage:[UIImage imageNamed:@"animOpen.png"] forState:UIControlStateNormal];
        [snow startFull];
    }
    else
    {
        [sender setBackgroundImage:[UIImage imageNamed:@"animClose.png"] forState:UIControlStateNormal];
        
        [snow endFull];
    }
    sender.selected = !sender.selected;
}
#pragma mark MusicPlayDelegate方法
/**
 *	@brief	MusicPlayDelegate方法 开始播放
 *
 *	@param 	_player
 */
- (void)musicDidStartPlay:(BanBu_PersonMusicPlay *)_player
{
    [_playbtn setMode:MusicPlayPlayingMode];
}
/**
 *	@brief	MusicPlayDelegate方法 结束播放
 *
 *	@param 	_player
 */
- (void)musicDidEndPlay:(BanBu_PersonMusicPlay *)_player
{
    [_playbtn setMode:MusicPlayStopedMode];
}

- (IBAction)seeFans:(id)sender
{
    [self seeHisFriend:@"f"];
}

- (IBAction)seeFollowing:(id)sender
{
    [self seeHisFriend:@"g"];
    
}
-(void)seeHisFriend:(NSString *)_relationShip
{
    NSLog(@"%d",_delegate.retainCount);
    BanBu_HisFriendViewController *hisfriend = [[BanBu_HisFriendViewController alloc] init];
    hisfriend.userid = [_dataSource valueForKey:@"userid"];
    hisfriend.relationShip = _relationShip;
    UIViewController *v = (UIViewController *)_delegate;
    
    [v.navigationController pushViewController:hisfriend animated:YES];
    //    [hisfriend release];
}
- (void)dealloc
{
    if([self isSelf]){ [[NSNotificationCenter defaultCenter] removeObserver:self name:Notification_FloatImage_Change object:nil]; }
    
    [_player destoryPlay];
    [_playbtn release],_player = nil;
    if(snow) {
        [snow endFull];
        [snow removeFromSuperview];
        [snow release];
    }
    self.snowImageURL = nil;
    [_waveImage destoryTimerMove];
    [_waveImage release];
    [_containerView release];
    [_verified release];
    [_sex release];
    [_starAndAge release];
    [_distance release];
    [_laseOnline release];
    [_fensiBtn release];
    [_guanzhuBtn release];
    [_fensiNum release];
    [_guanzhuNum release];
    [_vip release];
    [_ufaceImage release];
    [_actionButton release];
    [_albumButton release];
    [_profileButton release];
    [super dealloc];
}
@end
